Skip to content

Pitfall: struct-returning calls into ApolloOS stack-call libraries corrupt the library base - #60

Open
WDrijver wants to merge 1 commit into
mainfrom
pitfall/stackcall-struct-return-a0
Open

WDrijver wants to merge 1 commit into
mainfrom
pitfall/stackcall-struct-return-a0

Conversation

@WDrijver

Copy link
Copy Markdown
Contributor

What

A new OS pitfall, os/pitfalls/stackcall-struct-return-a0.md.

On m68k, ApolloOS's genmodule link-library stubs load the library base into %a0:

cexp:   move.l  StdCBase,a0
        jmp     -372(a0)

GCC passes the hidden result pointer of a struct-returning function in %a0 too. So these lose their result and write it over the start of the library base:

  • ldiv, div, lldiv, imaxdiv;
  • every double complex function;
  • cexp(), which GCC emits by itself for sin(x) + cos(x) of one x.

The caller reads garbage, and the program crashes a few seconds later.

The ApolloOS fix is tracked as ApolloTeam-dev/ApolloOS.R10#6. A pitfall even though AKB §5 asks for an issue: ApolloOS releases up to R9.6 ship the bug, and the pitfall gives the workarounds for code built against them.

Verified workaround: -fno-builtin-sin -fno-builtin-cos makes GCC 6.5 emit sin/cos instead of cexp. Checked with nm on the compiled Zune clock source.

Found during

The ApolloOS R10 port. Zune's clock in Prefs/Time and Utilities/Clock crashed the V4 (Software Failure, PC 0x26, supervisor mode). A test that only calls ldiv(17,5) and cexp(0) printed nothing and exited with RC 16688456. After the fix (base in %a1, %a0 kept) it prints 3 rem 2 and 1 + 0i, and the clocks run without a failure.

Knowledge base files relied on

  • os/pitfalls/library-globals-shared-between-tasks.md (format, related)
  • CONTRIBUTING.md (folder, frontmatter, evidence table)

Local checks: check_frontmatter, check_links and check_saga_addresses pass.

🤖 Generated with Claude Code

The m68k genmodule stubs load the library base into %a0, which GCC uses
for the struct-return pointer: ldiv/div/complex functions (and cexp,
which GCC emits for sin+cos of one value) lose their result and corrupt
the library base. ApolloOS.R10 issue #6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@WDrijver
WDrijver requested a review from TuKo1982 as a code owner November 17, 2026 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant